﻿@page "/pallets/mainplan/{MainPlanId}"
@inject IMainPlanService MainPlanService
@inject IPackingListService PackingListService;
@inject IPalletService PalletService;
<PageTitle>装箱信息</PageTitle>
<div style="position: sticky; top: 3.5rem; z-index: 1; background-color: White" class="mb-3">
    <div class="row">
        <div class="col-sm-5">
            <button class="btn btn-outline-success ms-3" @onclick="@(() => { Idx = Idx != 0 ? Idx - 1 : 0; })">上一个</button>
            <!--有时候托盘不是从1开始-->
            <span style="font-size: large">@PalletNumber 号托盘：</span>
            <!--展示托盘的QRcode-->
            <img src=@($"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data={QrCodeInfo}") data-origin=@($"https://api.qrserver.com/v1/create-qr-code/?size=150x150&data={QrCodeInfo}") alt="" class="mb-2">
            <button class="btn btn-outline-success ms-3" @onclick="@(() => { Idx = Idx != palletDtos.Count-1 ? Idx + 1 : palletDtos.Count-1; })">下一个</button>
        </div>
        <div class="col-sm-5">
            <h5 style="font-weight: bold">
                <span>@mainPlanDto.Number</span>
                <span>@mainPlanDto.Name</span>
            </h5>
            <div class="row">
                <div class="col-sm-3" style="width: 95px">烟罩类型:</div>
                <div class="col-sm-9">
                    <span>@packingListDto.Product</span>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-3" style="width: 95px">装箱形式:</div>
                <div class="col-sm-9">
                    <span>@packingListDto.PackingType</span>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-3" style="width: 95px">发货形式:</div>
                <div class="col-sm-9">
                    <span>@packingListDto.DeliveryType</span>
                </div>
            </div>
        </div>
    </div>
</div>
<table class="table table-hover" style="table-layout: fixed; word-break: break-all;">
    <!--sticky固定位置，top: 12.5rem-->
    <thead style="position: sticky; top: 13rem; z-index: 1; background-color: White">
        <tr>
            <th style="width: 65px">托盘号</th>
            <th>产品编号</th>
            <th>包装长</th>
            <th>包装宽</th>
            <th>包装高</th>
            <th>毛重</th>
            <th>净重</th>
            <th>备注</th>
            <th>型号</th>
            <th>产品长</th>
            <th>产品宽</th>
            <th>产品高</th>
            <th>库位</th>
            <th>入库日期</th>
            <th>发货日期</th>
        </tr>
    </thead>
    <tbody>
        @if (palletDtos.Count == 0)
        {
            <p>...</p>
        }
        else
        {
            @foreach (var pallet in palletDtos)
            {
                <PalletRow PalletDto="pallet" />
            }
        }
    </tbody>
</table>


@code {
    [Parameter]
    public string MainPlanId { get; set; }
    //查PackingListDto，再查Pallets，使用table展示Pallets
    private MainPlanDto mainPlanDto = new MainPlanDto();
    private PackingListDto packingListDto = new PackingListDto();
    private List<PalletDto> palletDtos = new List<PalletDto>();

    private int idx;
    public int Idx
    {
        get => idx;
        set
        {
            idx = value;
            GetPalletQrCodeInfo();
        }
    }
    public string PalletNumber { get; set; }
    public string QrCodeInfo { get; set; }
    protected override async Task OnInitializedAsync()
    {
        var mpResponse = await MainPlanService.GetFirstOrDefaultAsync(Guid.Parse(MainPlanId));
        if (mpResponse.Status) mainPlanDto = mpResponse.Result;

        var pklResponse = await PackingListService.GetPackingListDtoByMainPlanIdAsync(Guid.Parse(MainPlanId));
        if (pklResponse.Status)
        {
            packingListDto = pklResponse.Result;
            var pltResponse = await PalletService.GetPalletsByPackingListIdAsync(packingListDto.Id.Value);
            if (pltResponse.Status)
            {
                palletDtos = pltResponse.Result;
            }
        }
        GetPalletQrCodeInfo();
    }

    private void GetPalletQrCodeInfo()
    {
        if(palletDtos.Count==0)return;//如果没有托盘，不显示二维码
        var itemDto = palletDtos[Idx];
        PalletNumber = itemDto.PalletNumber.ToString();
        QrCodeInfo = $"pallet_{mainPlanDto.Number}_{itemDto.PalletNumber}_{itemDto.MtlNumber}_{itemDto.Type}_{itemDto.Id}";
    }
}
